package com.zy.orders.web.controllers;

import com.zy.orders.service.OrdersService;
import com.zy.web.bean.OrdersDTO;
import com.zy.web.bean.ResponseResult;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @program: shopping147_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-01 16:03
 */
@RestController
@RequestMapping("/orders/operation")
@AllArgsConstructor
public class OrdersController {

    private final OrdersService ordersService;

    @PostMapping("/create")
    public ResponseEntity<ResponseResult> createOrder(
            @RequestHeader("X-User-ID") Long userId,
            @RequestBody OrderCreateRequest request) { // 接收新的请求体对象
        try {
           OrdersDTO order = ordersService.createOrder(userId, request.getOrdersDTO(), request.getOrderItemsDTOList(), request.getCartIds());
           return new ResponseEntity<>(ResponseResult.ok("订单创建成功").setData(order), HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(ResponseResult.error("订单创建失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
